package com.meisupic.copyright.amdin.controller;

import com.alibaba.fastjson.JSON;
import com.meisupic.copyright.common.WorksProcessDetailEnum;
import com.meisupic.copyright.domain.bo.SysUser;
import com.meisupic.copyright.domain.query.WorksQuery;
import com.meisupic.copyright.domain.vo.CategroyVo;
import com.meisupic.copyright.domain.vo.CopyRightWorksRowVo;
import com.meisupic.copyright.domain.vo.WorksAuditVo;
import com.meisupic.copyright.service.ICopyRightWorksService;
import com.meisupic.copyright.support.Message;
import com.meisupic.copyright.support.PageResponse;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.web.PageableDefault;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author lanjian
 * @Date 2018/2/20 下午12:08
 */
@RestController
@RequestMapping("/admin/works")
public class WorksController {

  @Autowired
  ICopyRightWorksService copyRightWorksService;

  @GetMapping(value = "/page")
  public PageResponse<CopyRightWorksRowVo> queryPage(WorksQuery worksQuery,
      @PageableDefault(sort = {"createTime"}, direction = Direction.DESC) Pageable pageable) {
    PageRequest pageRequest = new PageRequest(pageable.getPageNumber() - 1, pageable.getPageSize(),
        pageable.getSort());
    Page<CopyRightWorksRowVo> worksPage = copyRightWorksService
        .findPageCopyRightWorkRowVO(worksQuery, pageRequest);
    PageResponse<CopyRightWorksRowVo> pageResponse = new PageResponse<>(
        worksPage.getTotalElements(), worksPage.getContent());
    return pageResponse;
  }

  /**
   * 根据id查找作品
   */
  @GetMapping("getWorksVoById")
  public CopyRightWorksRowVo getWorksVoById(Long id) {
    CopyRightWorksRowVo worksRowVo = copyRightWorksService.findSingleWorksVo(id);
    return worksRowVo;
  }

  @GetMapping("getCategoryList")
  public List<CategroyVo> getCategoryList() {
    return copyRightWorksService.findCategoryVoList();
  }

  @GetMapping("getWorksProcessDetail")
  public Map<String, String> getWorkProcessDetail() {
    Map<String, String> result = new LinkedHashMap<>();
    WorksProcessDetailEnum[] worksProcessDetailEnums = WorksProcessDetailEnum.values();
    for (WorksProcessDetailEnum worksProcessDetailEnum : worksProcessDetailEnums) {
      result.put(worksProcessDetailEnum.name(), worksProcessDetailEnum.getDesc());
    }
    return result;
  }

  /**
   * 审核通过
   */
  @PostMapping("pass")
  public Message auditPass(WorksAuditVo worksAuditVo) {
    SysUser user = (SysUser) SecurityContextHolder.getContext()
        .getAuthentication()
        .getPrincipal();
    worksAuditVo.setAuditor(user.getUsername());
    copyRightWorksService.doPass(worksAuditVo);
    return Message.success(null);
  }

  /**
   * 审核失败
   */
  @PostMapping("refuse")
  public Message auditRefuse(WorksAuditVo worksAuditVo) {
    SysUser user = (SysUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    worksAuditVo.setAuditor(user.getUsername());
    copyRightWorksService.doRefuse(worksAuditVo);
    return Message.success(null);
  }

  /**
   * 修改评分
   */
  @PostMapping("updateScore")
  public Message updateScore(Long id, Integer score) {
    copyRightWorksService.updateScore(id, score);
    return Message.success(null);
  }

  /**
   * 修改评分
   */
  @PostMapping("updateKeyWords")
  public Message updateKeyWords(Long id, String keywords) {
    copyRightWorksService.updateScore(id, keywords);
    return Message.success(null);
  }

  /**
   * 修改作品类型
   */
  @PostMapping("updateWorksType")
  public Message updateWorksType(Long id, String worksType) {
    if (StringUtils.isEmpty(worksType)) {
      worksType = "othersProductRegist";
    }
    copyRightWorksService.updateWorksType(id, worksType);
    return Message.success(null);
  }

  /**
   * 重置错误计数器
   */
  @GetMapping("reset")
  public Message reset() {
    return Message.success(copyRightWorksService.resetErrorCount());
  }

  /**
   * 下架作品
   */
  @GetMapping("offImage")
  public Message offImage(Long id) {
    copyRightWorksService.offImage(id);
    return Message.success(null);
  }

  @PostMapping("batchRefuse")
  public Message batchRefuse(@RequestBody WorksAuditVo worksAuditVo) {
    SysUser user = (SysUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    for (Long id : worksAuditVo.getIds()) {
      WorksAuditVo auditVo = new WorksAuditVo();
      auditVo.setRefuseReason(worksAuditVo.getRefuseReason());
      auditVo.setAuditor(user.getUsername());
      auditVo.setId(id);
      copyRightWorksService.doRefuse(auditVo);
    }
    return Message.success(null);
  }
}
